package com.yonyou.crm.sys.tenant.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.sys.tenant.entity.SysTenantVO;

@Service
public class SysTenantBizServiceImpl extends BaseBizServiceImpl<SysTenantVO>{
	
	@Autowired
	private EnumBizServiceImpl enumService;
	
	public SysTenantVO getSysTenantById(Long id){
		SysTenantVO sysTenant =  super.getVOById(SysTenantVO.class, id);
		List<SysTenantVO> sysTenantList = transTenant(Arrays.asList(sysTenant));
		if(sysTenantList != null && sysTenantList.size() > 0){
			sysTenant = sysTenantList.get(0);
		}
		return sysTenant;
	}
	
	public SysTenantVO updateSysTenant(SysTenantVO sysTenant){
		return super.updateVO(sysTenant);
	}
	
	private List<SysTenantVO> transTenant(List<SysTenantVO> tenantList){
		if(tenantList != null && tenantList.size() > 0) {
			List<FieldRelation<SysTenantVO>> relations = new ArrayList<FieldRelation<SysTenantVO>>();
			relations.add(new EnumFieldRelation<SysTenantVO>(SysTenantVO.class, "companyType", "companyTypeName", enumService, "company_type"));
			new FieldRelationHandler<SysTenantVO>().handler(tenantList, relations);
		}
		return tenantList;
	}
}
